{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0b68677",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b1df78ba",
   "metadata": {},
   "source": [
    "# Prepare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdbc4ba5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 1: 导入依赖库、绘图/可视化 ===\n",
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "import scipy.stats as stats # 统计模块\n",
    "import scipy  # 科学计算\n",
    "# import pymysql  # 导入数据库模块\n",
    "\n",
    "from datetime import datetime # 时间模块\n",
    "import statsmodels.formula.api as smf  # OLS regression\n",
    "\n",
    "# import pyreadr # read RDS file\n",
    "\n",
    "from matplotlib import style  # 绘图\n",
    "import matplotlib.pyplot as plt  # 画图模块\n",
    "import matplotlib.dates as mdates  # 绘图\n",
    "\n",
    "\n",
    "from matplotlib.font_manager import FontProperties # 作图中文\n",
    "from pylab import mpl\n",
    "#mpl.rcParams['font.sans-serif'] = ['SimHei']\n",
    "#plt.rcParams['font.family'] = 'Times New Roman'\n",
    "mpl.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # Mac系统中文字体\n",
    "plt.rcParams['font.family'] = 'Arial Unicode MS'\n",
    "\n",
    "#输出矢量图 渲染矢量图\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'svg'\n",
    "\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "\n",
    "#设置行不限制数量\n",
    "#pd.set_option('display.max_rows',None)\n",
    "\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d94692a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 2: 读取数据、数据清洗/转换 ===\n",
    "data = pd.read_csv('datasets/000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day', inplace = True)\n",
    "data.sort_values(by = ['Day'], ascending=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39913860",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 3: 通用计算/执行 ===\n",
    "data_new = data['1995-01':'2024-07'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose'] = pd.to_numeric(data_new['Preclose'])\n",
    "# 计算000001上证指数日收益率 两种：\n",
    "data_new['Raw_return'] = data_new['Close'] / data_new['Preclose'] - 1\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "991bfb63",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 4: 通用计算/执行 ===\n",
    "Month_data = data_new.resample('ME')['Raw_return'].apply(lambda x: (1+x).prod()-1).to_frame()\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17c97e06",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 5: 通用计算/执行 ===\n",
    "Month_data = data_new.resample('ME')['Raw_return'].apply(lambda x: np.prod(1+x)-1).to_frame()\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d540074",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 6: 通用计算/执行 ===\n",
    "Quarter_data = data_new.resample('QE')['Raw_return'].apply(lambda x: np.prod(1+x)-1).to_frame()\n",
    "Quarter_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b163e6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 7: 通用计算/执行 ===\n",
    "Year_data = data_new.resample('YE')['Raw_return'].apply(lambda x: np.prod(1+x)-1).to_frame()\n",
    "Year_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd1e9e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 8: 通用计算/执行 ===\n",
    "# 更换列名字\n",
    "Month_data.columns = ['Return']\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3482ccaa",
   "metadata": {},
   "source": [
    "# Think 要学会分析数据，思考数据，挖掘数据背后的故事\n",
    "- 为什么股票市场的回报率如此之高？\n",
    "- 为什么在股票市场回报率如此高的情况下，似乎也没有什么人能在股票市场挣钱？\n",
    "- 我们能够提前预测股票市场回报率么？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e7e8fe3",
   "metadata": {},
   "source": [
    "## 数据可视化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8abdc713",
   "metadata": {},
   "source": [
    "### 月度数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a2f0191",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 9: 绘图/可视化 ===\n",
    "# 画图\n",
    "fig, ax = plt.subplots(figsize=(10, 5)) # 图片比例\n",
    "ax.plot(\n",
    "    'Return',  # 要画图的变量名\n",
    "    '.-',  # 线的类型\n",
    "    color = '#4876FF',  # 线的颜色 RGB\n",
    "    label = 'Return',  # 这个是线的类别，主要是在多条线画图的时候，起到区别的作用，单条线这个没有影响\n",
    "    linewidth = 1,  # 线的粗细\n",
    "    data = Month_data['1995-01-01':'2024-07-31'])  # 画图的数据\n",
    "ax.set_title(\"中国股票市场收益率 China's Stock Market\") # 画图的标题\n",
    "ax.set_xlabel('month') # 画图的x轴名称\n",
    "plt.ylabel('Return') # 画图的y轴名称\n",
    "\n",
    "# 设置x轴的日期格式\n",
    "date_format = mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(date_format)\n",
    "\n",
    "# # 设置x轴的刻度间隔\n",
    "ax.xaxis.set_major_locator(mdates.YearLocator())\n",
    "\n",
    "# 旋转x轴标签以防止重叠\n",
    "plt.xticks(rotation=90)\n",
    "\n",
    "# 添加图例\n",
    "plt.legend(loc='upper left', frameon=False, fontsize=10)\n",
    "fig.savefig('Monthly_return.pdf', bbox_inches='tight')# 更改输出图片格式 jpg\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec78229e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 10: 绘图/可视化 ===\n",
    "help(plt.plot)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72a2a8c8",
   "metadata": {},
   "source": [
    "# 季度数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93b9fce1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 11: 绘图/可视化 ===\n",
    "# 画图\n",
    "fig, ax = plt.subplots(figsize=(10, 5)) # 图片比例\n",
    "ax.plot(\n",
    "    'Raw_return',  # 要画图的变量名\n",
    "    '.-',  # 线的类型\n",
    "    color = '#4876FF',  # 线的颜色 RGB\n",
    "    label = 'Return',  # 这个是线的类别，主要是在多条线画图的时候，起到区别的作用，单条线这个没有影响\n",
    "    linewidth = 1,  # 线的粗细\n",
    "    data = Quarter_data['1995-01-01':'2024-07-31'])  # 画图的数据\n",
    "ax.set_title(\"China's Stock Market\") # 画图的标题\n",
    "ax.set_xlabel('month') # 画图的x轴名称\n",
    "plt.ylabel('Return') # 画图的y轴名称\n",
    "\n",
    "# 设置x轴的日期格式\n",
    "date_format = mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(date_format)\n",
    "\n",
    "# # 设置x轴的刻度间隔\n",
    "ax.xaxis.set_major_locator(mdates.YearLocator())\n",
    "\n",
    "# 旋转x轴标签以防止重叠\n",
    "plt.xticks(rotation=90)\n",
    "\n",
    "# 添加图例\n",
    "plt.legend(loc='upper left', frameon=False, fontsize=10)\n",
    "fig.savefig('Quaterly_return.pdf', bbox_inches='tight')# 更改输出图片格式 jpg\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4fe3e8d",
   "metadata": {},
   "source": [
    "# 年度数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8e3b27e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 12: 绘图/可视化 ===\n",
    "# 画图\n",
    "fig, ax = plt.subplots(figsize=(10, 5)) # 图片比例\n",
    "ax.plot(\n",
    "    'Raw_return',  # 要画图的变量名\n",
    "    '.-',  # 线的类型\n",
    "    color = '#4876FF',  # 线的颜色 RGB\n",
    "    label = 'Return',  # 这个是线的类别，主要是在多条线画图的时候，起到区别的作用，单条线这个没有影响\n",
    "    linewidth = 1,  # 线的粗细\n",
    "    data = Year_data['1995-01-01':'2024-07-31'])  # 画图的数据\n",
    "ax.set_title(\"China's Stock Market\") # 画图的标题\n",
    "ax.set_xlabel('month') # 画图的x轴名称\n",
    "plt.ylabel('Return') # 画图的y轴名称\n",
    "\n",
    "# 设置x轴的日期格式\n",
    "date_format = mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(date_format)\n",
    "\n",
    "# # 设置x轴的刻度间隔\n",
    "ax.xaxis.set_major_locator(mdates.YearLocator())\n",
    "\n",
    "# 旋转x轴标签以防止重叠\n",
    "plt.xticks(rotation=90)\n",
    "\n",
    "# 添加图例\n",
    "plt.legend(loc='upper left', frameon=False, fontsize=10)\n",
    "fig.savefig('Yearly_return.pdf', bbox_inches='tight')# 更改输出图片格式 jpg\n",
    "plt.show();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dfac194",
   "metadata": {},
   "source": [
    "# 我们需要思考下面的问题："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "987d3a9a",
   "metadata": {},
   "source": [
    "- Is the stock market return constant or time-varying?\n",
    "  - constant\n",
    "    - $ r_t = a + \\epsilon_{t} $, $\\epsilon$ 是随机扰动项\n",
    "  - varying\n",
    "    - $ r_t = b * k_{t-1} + \\epsilon_{t} $\n",
    "    - 我们找到$k_{t-1}$提前预知 $r_{t}$\n",
    "- Do you think the stock market return (or we can call it \"equity premium\") is too high?\n",
    "  - 股权溢价，“股权**溢价**之谜” equity premium puzzle\n",
    "- Understand the time-patterns of China's stock market return."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da456350",
   "metadata": {},
   "outputs": [],
   "source": [
    "# === 单元格 13: 绘图/可视化 ===\n",
    "# 画图\n",
    "fig, ax = plt.subplots(figsize=(10, 5)) # 图片比例\n",
    "ax.plot(\n",
    "    'Raw_return',  # 要画图的变量名\n",
    "    '.-',  # 线的类型\n",
    "    color = '#4876FF',  # 线的颜色 RGB\n",
    "    label = 'Return',  # 这个是线的类别，主要是在多条线画图的时候，起到区别的作用，单条线这个没有影响\n",
    "    linewidth = 1,  # 线的粗细\n",
    "    data = data_new['1995-01-01':'2024-07-31'])  # 画图的数据\n",
    "ax.set_title(\"China's Stock Market\") # 画图的标题\n",
    "ax.set_xlabel('month') # 画图的x轴名称\n",
    "plt.ylabel('Return') # 画图的y轴名称\n",
    "\n",
    "# 设置x轴的日期格式\n",
    "date_format = mdates.DateFormatter('%Y')\n",
    "ax.xaxis.set_major_formatter(date_format)\n",
    "\n",
    "# # 设置x轴的刻度间隔\n",
    "ax.xaxis.set_major_locator(mdates.YearLocator())\n",
    "\n",
    "# 旋转x轴标签以防止重叠\n",
    "plt.xticks(rotation=90)\n",
    "\n",
    "# 添加图例\n",
    "plt.legend(loc='upper left', frameon=False, fontsize=10)\n",
    "fig.savefig('Daily_return.pdf', bbox_inches='tight')# 更改输出图片格式 jpg\n",
    "plt.show();"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
